<?php

namespace app\admin\controller\keesuishoupai;

use app\common\controller\Backend;
use think\Db;
/**
 * 
 *
 * @icon fa fa-dashboard
 */
class Dashboard extends Backend
{
    public function index()
    {
        //用户总数量
        $totalUserCount = \app\common\model\User::count();
        //总上报量
        $totalReportCount = \app\admin\model\keesuishoupai\Report::count();
        //待审核上报
        $waitReportCount = \app\admin\model\keesuishoupai\Report::where('status',0)->count();
        //今日上报量
        $todayReportCount = \app\admin\model\keesuishoupai\Report::where('createtime','>', strtotime('yesterday'))->count();
        //总赠送积分
        $totalScoreCount = \app\admin\model\keesuishoupai\Report::where('status',1)->sum("score");

        //待发货订单
        $totalOrderCount1 = \app\admin\model\keesuishoupai\Order::where('status',1)->count();

        if ($this->request->isPost()) {
            $date = $this->request->post('date', '');
            list($category, $data) = $this->getEchartsData($date);
            $this->success('', '',['category' => $category, 'data' => $data]);
        }
        //上报图标
        list($category, $data) = $this->getEchartsData('');
        $this->assignconfig('echartsCategory', $category);
        $this->assignconfig('echartsData', $data);

        $this->view->assign("totalScoreCount", $totalScoreCount);
        $this->view->assign("totalUserCount", $totalUserCount);
        $this->view->assign("totalReportCount", $totalReportCount);
        $this->view->assign("waitReportCount", $waitReportCount);
        $this->view->assign("todayReportCount", $todayReportCount);
        $this->view->assign("totalOrderCount1", $totalOrderCount1);
        
        return $this->view->fetch();
    }

    /**
     * 获取发文统计数据
     * @param string $date
     * @return array
     */
    protected function getEchartsData($date = '')
    {
        if ($date) {
            list($start, $end) = explode(' - ', $date);
            $starttime = strtotime($start);
            $endtime = strtotime($end);
        } else {
            $starttime = \fast\Date::unixtime('day', 0, 'begin');
            $endtime = \fast\Date::unixtime('day', 0, 'end');
        }
        $totalseconds = $endtime - $starttime;

        $format = '%Y-%m-%d';
        if ($totalseconds > 86400 * 30 * 2) {
            $format = '%Y-%m';
        } else {
            if ($totalseconds > 86400) {
                $format = '%Y-%m-%d';
            } else {
                $format = '%H:00';
            }
        }
        $list = \app\admin\model\keesuishoupai\Report::where('createtime', 'between time', [$starttime, $endtime])
            ->field('createtime,status, COUNT(*) AS nums,SUM(score) AS sum_score, MIN(createtime) AS min_createtime, MAX(createtime) AS max_createtime,DATE_FORMAT(FROM_UNIXTIME(createtime), "' . $format . '") AS create_date')
            ->group('create_date')
            ->select();
        if ($totalseconds > 84600 * 30 * 2) {
            $starttime = strtotime('last month', $starttime);
            while (($starttime = strtotime('next month', $starttime)) <= $endtime) {
                $column[] = date('Y-m', $starttime);
            }
        } else {
            if ($totalseconds > 86400) {
                for ($time = $starttime; $time <= $endtime;) {
                    $column[] = date("Y-m-d", $time);
                    $time += 86400;
                }
            } else {
                for ($time = $starttime; $time <= $endtime;) {
                    $column[] = date("H:00", $time);
                    $time += 3600;
                }
            }
        }
        $reportData = $scoreData = array_fill_keys($column, 0);
        foreach ($list as $k => $v) {
            $reportData[$v['create_date']] = $v['nums'];
            $scoreData[$v['create_date']] = $v['sum_score'];
        }
        $dataList[0] = [
            'name'      => '上报次数',
                'type'      => 'line',
                'smooth'    => true,
                'areaStyle' => [],
                'data'      => array_values($reportData)
        ];
        $dataList[1] = [
            'name'      => '赠送积分',
                'type'      => 'line',
                'smooth'    => true,
                'areaStyle' => [],
                'data'      => array_values($scoreData)
        ];
        return [$column, $dataList];
    }
}
